<?php

namespace app\modules\web\controllers;

use yii\web\Controller;
use \app\modules\web\controllers\common\BaseController;
use \app\models\User;
use \app\common\services\ConstantMapService;
use \app\common\services\UrlService;
use \app\models\log\AppAccessLog;
class AccountController extends BaseController
{
    //用户列表
    public function actionIndex()
    {
    	$this->layout="main";
        $status=intval($this->get("status",ConstantMapService::$status_default));
        $mix_kw=trim($this->get("mix_kw",""));
        $query=User::find();
        $p=($this->get("p",1));
        if($status > ConstantMapService::$status_default){
            $query->andWhere(["status"=>$status]);
        }
        if($mix_kw){
            $name=["LIKE","nickname","%".$mix_kw."%",false];
            $mobile=["LIKE","mobile","%".$mix_kw."%",false];
            $query->andWhere(["OR",$name,$mobile]);

        }
        //分页
        $total_all=$query->count();
        $page_size=2;
        $total_page=ceil($total_all/$page_size);
        $list=$query->orderBy(['uid'=>SORT_DESC])
                ->offset( ($p-1)*$page_size )
                ->limit($page_size)
                ->all();

        return $this->render('index',["list"=>$list,"status_mapping"=>ConstantMapService::$status_mapping,"page"=>
            [
                'total_all'=>$total_all,
                'total_page'=>$total_page,
                'page_size'=>$page_size,
                'p'=>$p
            ] ]);
    }
    //用户编辑或者添加
    public function actionSet(){
    	$this->layout="main";
        if(\Yii::$app->request->isGet){
            $id=intval($this->get("id",0));
            $info=null;
            if($id){
                $info=User::find()->where(['uid'=>$id])->one();

            }
            return $this->render('set',['info'=>$info]);
        }
        $id=$this->post('id',0);
        $nickname=trim($this->post("nickname",""));
        $mobile=trim($this->post("mobile",""));
        $email=trim($this->post("email",""));
        $login_name=trim($this->post("login_name",""));
        $login_pwd=trim($this->post("login_pwd",""));
        $date_now=date("Y-m-d H:i:s");
       
        $user=User::find()->where(['uid'=>$id])->one();
        //检测是否已经存在
        $is_find=User::find()->where(['login_name'=>$login_name])->andWhere(['!=','uid',$id])->count();
        if($is_find){
            return $this->renderJson([],"用户已经存在",-1);
        }
        if($user){//编辑
            $new_account=$user;
        }else{//添加
            $new_account=new User();
            $new_account->setSalt();
            $new_account->created_time=$date_now;
        }
        $new_account->nickname=$nickname;
        $new_account->mobile=$mobile;
        $new_account->email=$email;
        $new_account->login_name=$login_name;
        $new_account->avatar=ConstantMapService::$default_avatar;
        if($login_pwd!="******"){
            $new_account->setPwd($login_pwd);
        }
        $new_account->updated_time=$date_now;
        $new_account->save(0);
        return $this->renderJson([],"操作成功~");

    }
    //用户信息
    public function actionInfo(){
    	$this->layout="main";
    	$id=intval($this->get('id',0));
        $reback_url=UrlService::buildWebUrl("/account/index");
        if(!$id){
            return $this->redirect($reback_url);
        }
        $info=User::find()->where(['uid'=>$id])->one();
        if(!$info){
            return $this->redirect($reback_url);

        }
        $appAccessLog=AppAccessLog::find()->where(['uid'=>$id])->all();

        return $this->render('info',['info'=>$info,"appAccessLog"=>$appAccessLog]);

    }
    //用户操作
    public function actionOps(){
        if(!\Yii::$app->request->isPost){
            return $this->renderJson([],"系统繁忙~请稍后再试~",-1);
        }
        $uid=intval($this->post("uid",0));
        $act=trim($this->post("act",""));
        if(!$uid){
            return $this->renderJson([],"操作有误~",-1);
        }
        if(!in_array($act,["remove","recover"])){
            return $this->renderJson([],"操作有误~",-1);
        }
        $user_info=User::find()->where(["uid"=>$uid])->one();
        if(!$user_info){
            return $this->renderJson([],"用户不存在~",-1);
        }
        switch($act){
            case "remove":
                $user_info->status=0;
                break;
            case "recover":
                $user_info->status=1;
                break;
                
        }
        $user_info->updated_time=date("Y-m-d H:i:s");
        $user_info->update(0);
        return $this->renderJson([],"编辑成功~");
    }
}
